<template>
  <div class="box">
    <div class="content">
      <div class="container">
        <div class="title">
          <img src="./p1.jpg" alt="" />
        </div>
        <div class="body">~ 开始努力写代码了 ~</div>
        <div class="buttom">
          <div>
            <input type="text" placeholder="添加要做的事..." v-model="val" />
          </div>
          <div @click="addArr">
            <img src="./p2.jpg" alt="" />
            <button class="aaa">提交</button>
          </div>
        </div>
        <div class="add">
          <ul>
            <li v-for="(item, index) in arr" :key="item.id">
              <input
                type="checkbox"
                v-model="isCheckout"
                :value="item"
                @change="checked(item.id)"
              />
              <span :class="{ active: item.id === selectBtn }">{{
                item.content
              }}</span>
              <div @click="del(index)">X</div>
            </li>
          </ul>
        </div>
        <div class="filder">
          <button>全部</button>
          &nbsp;&nbsp;
          <span>{{ count }}条事项</span>
          &nbsp;&nbsp;
          <button>已完成:{{ isChecked }}</button>
          &nbsp;&nbsp;
          <button>未完成:{{ noChecked }}</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
// 初始数据
let arr = ref([]);

// input的输入值
let val = ref();

// 事项
let count = ref(0);

// 添加事件
let addArr = function () {
  // setTimeout(() => {
  // console.log(val.value);
  console.log(arr.value);
  arr.value.unshift({
    content: val.value,
    id: Math.random().toString(32).substr(2),
  });
  // })
  count.value = arr.value.length;
  localStorage.setItem("arr-data", JSON.stringify(arr.value));
  val.value = "";
};

// 删除事件
let del = function (index) {
  let target = arr.value.splice(index, 1);
  console.log(target);
  localStorage.setItem("arr-data", JSON.stringify(arr.value));
  isCheckout.value = isCheckout.value.filter(
    (item) => item.id !== target[0].id
  );
  isChecked.value = isCheckout.value.length;
  count.value = arr.value.length;
  console.log(arr.value.length, isChecked.value);
};

// 单选框是否被选中
let isCheckout = ref([]);
console.log(isCheckout.value);

//
let selectBtn = ref("");
// 已完成
let isChecked = ref(0);

let checked = function (a) {
  console.log(a);
  selectBtn.value = a;
  console.log(selectBtn.value);
  isChecked.value = isCheckout.value.length;
};

// 未完成
let noChecked = computed(() => {
  return arr.value.length - isChecked.value;
});
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

body {
  background: linear-gradient(90deg, #b3f6e9, #1baea7);
}

.box {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  text-align: center;
}

.container {
  width: 500px;
  /* height: 330px; */
  background-color: #f2f2f2;
  border-radius: 10px;
  padding-bottom: 30px;
}

.title {
  overflow: hidden;
}

.title img {
  width: 200px;
  margin: 30px 0 30px 0;
}

.body {
  color: #00bfff;
  font-size: 26px;
}

.add {
  margin-bottom: 30px;
}

.add div {
  cursor: pointer;
}

.add li {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0;
}

.add li span {
  display: inline-block;
  width: 320px;
  text-align: left;
  margin-left: 15px;
}

.buttom {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
}

.buttom input {
  width: 280px;
  height: 30px;
  outline: none;
  border: none;
  border-bottom: 4px dashed #00bfff;
  background-color: #f2f2f2;
  font-size: 20px;
}

.buttom img {
  width: 70px;
  margin: 20px 0 0 20px;
  cursor: pointer;
}
.aaa {
  display: none;
}
</style>
